<?php

class UploadFile extends Think
{
public $maxSize = -1;
public $supportMulti = true;
public $allowExts = array();
public $allowTypes = array();
public $thumb   =  false;
public $thumbMaxWidth;
public $thumbMaxHeight;
public $thumbPrefix   =  'thumb_';
public $thumbSuffix  =  '';
public $thumbPath = '';
public $thumbFile		=	'';
public $thumbRemoveOrigin = false;
public $zipImages = false;
public $autoSub   =  false;
public $subType   = 'hash';
public $dateFormat = 'Ymd';
public $hashLevel =  1;
public $savePath = '';
public $autoCheck = true;
public $uploadReplace = false;
public $saveRule = '';
public $hashType = 'md5_file';
private $error = '';
private $uploadFileInfo ;
public $denyExts = array('exe','php','asp','aspx','bat','msi','vbs','js','cmd','scr','com','jar');
public $denyTypes = array();
public function __construct($maxSize='',$allowExts='',$allowTypes='',$savePath='',$saveRule='')
{
if(!empty($maxSize) &&is_numeric($maxSize)) {
$this->maxSize = $maxSize;
}
if(!empty($allowExts)) {
if(is_array($allowExts)) {
$this->allowExts = array_map('strtolower',$allowExts);
}else {
$this->allowExts = explode(',',strtolower($allowExts));
}
}
if(!empty($allowTypes)) {
if(is_array($allowTypes)) {
$this->allowTypes = array_map('strtolower',$allowTypes);
}else {
$this->allowTypes = explode(',',strtolower($allowTypes));
}
}
if(!empty($saveRule)) {
$this->saveRule = $saveRule;
}else{
$this->saveRule	=	C('UPLOAD_FILE_RULE');
}
$this->savePath = $savePath;
}
private function save($file)
{
$filename = $file['savepath'].$file['savename'];
if(!$this->uploadReplace &&is_file($filename)) {
$this->error	=	l('FILE_EXIST').$filename;
return false;
}
if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf')) &&false === getimagesize($file['tmp_name'])) {
$this->error = l('INVALID_IMAGE');
return false;
}
if(!move_uploaded_file($file['tmp_name'],auto_charset($filename,'utf-8','gbk'))) {
$this->error = L('SAVE_IMAGE_ERROR');
return false;
}
if($this->thumb &&in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png'))) {
$image =  getimagesize($filename);
if(false !== $image) {
$thumbWidth		=	explode(',',$this->thumbMaxWidth);
$thumbHeight		=	explode(',',$this->thumbMaxHeight);
$thumbPrefix		=	explode(',',$this->thumbPrefix);
$thumbSuffix = explode(',',$this->thumbSuffix);
$thumbFile			=	explode(',',$this->thumbFile);
$thumbPath    =  $this->thumbPath?$this->thumbPath:$file['savepath'];
import('ORG.Util.Image');
for($i=0,$len=count($thumbWidth);$i<$len;$i++) {
$thumbname	=	$thumbPath.$thumbPrefix[$i].substr($file['savename'],0,strrpos($file['savename'],'.')).$thumbSuffix[$i].'.'.$file['extension'];
Image::thumb($filename,$thumbname,'',$thumbWidth[$i],$thumbHeight[$i],true);
}
if($this->thumbRemoveOrigin) {
unlink($filename);
}
}
}
if($this->zipImags) {
}
return true;
}
public function upload($savePath ='')
{
if(empty($savePath))
$savePath = $this->savePath;
if(!is_dir($savePath)) {
if(is_dir(base64_decode($savePath))) {
$savePath	=	base64_decode($savePath);
}else{
if(!mkdir($savePath)){
$this->error  =  sprintf(L('UPLOAD_DIR_NOT_EXIST'),$savePath);
return false;
}
}
}else {
if(!is_writeable($savePath)) {
$this->error  =  sprintf(L('UPLOAD_DIR_NOT_WRITE'),$savePath);
return false;
}
}
$fileInfo = array();
$isUpload   = false;
$files	 =	 $this->dealFiles($_FILES);
foreach($files as $key =>$file) {
if(!empty($file['name'])) {
$file['key']          =  $key;
$file['extension']  = $this->getExt($file['name']);
$file['savepath']   = $savePath;
$file['savename']   = $this->getSaveName($file);
if($this->autoCheck) {
if(!$this->check($file))
return false;
}
if(!$this->save($file)) return false;
if(function_exists($this->hashType)) {
$fun =  $this->hashType;
$file['hash']   =  $fun(auto_charset($file['savepath'].$file['savename'],'utf-8','gbk'));
}
unset($file['tmp_name'],$file['error']);
$fileInfo[] = $file;
$isUpload   = true;
}
}
if($isUpload) {
$this->uploadFileInfo = $fileInfo;
return true;
}else {
$this->error  =  l('NOT_FILE_SELECTED');
return false;
}
}
private function dealFiles($files) {
$fileArray = array();
foreach ($files as $file){
if(is_array($file['name'])) {
$keys = array_keys($file);
$count	 =	 count($file['name']);
for ($i=0;$i<$count;$i++) {
foreach ($keys as $key)
$fileArray[$i][$key] = $file[$key][$i];
}
}else{
$fileArray	=	$files;
}
break;
}
return $fileArray;
}
protected function error($errorNo)
{
switch($errorNo) {
case 1:
$this->error = l('FILE_ERROR_1');
break;
case 2:
$this->error = l('FILE_ERROR_2');
break;
case 3:
$this->error = l('FILE_ERROR_3');
break;
case 4:
$this->error = l('FILE_ERROR_4');
break;
case 6:
$this->error = l('FILE_ERROR_6');
break;
case 7:
$this->error = l('FILE_ERROR_7');
break;
default:
$this->error = l('FILE_ERROR_UNKNOWN');
}
return ;
}
private function getSaveName($filename)
{
$rule = $this->saveRule;
if(empty($rule)) {
$saveName = $filename['name'];
}else {
if(function_exists($rule)) {
$saveName = $rule().'.'.$filename['extension'];
}else {
$saveName = $rule.'.'.$filename['extension'];
}
}
if($this->autoSub) {
$saveName   =  $this->getSubName($filename).'/'.$saveName;
}
return $saveName;
}
private function getSubName($file)
{
switch($this->subType) {
case 'date':
$dir   =  toDate(gmtTime(),$this->dateFormat);
break;
case 'hash':
default:
$name = md5($file['savename']);
$dir   =  '';
for($i=0;$i<$this->hashLevel;$i++) {
$dir   .=  $name{0}.'/';
}
break;
}
if(!is_dir($file['savepath'].$dir)) {
mk_dir($file['savepath'].$dir);
}
return $dir;
}
private function check($file) {
if($file['error']!== 0) {
$this->error($file['error']);
return false;
}
if(!$this->checkSize($file['size'])) {
$this->error = l('SIZE_NOT_MATCH');
return false;
}
if(!$this->checkType($file['type'])) {
$this->error = l('MIME_NOT_ALLOW');
return false;
}
if(!$this->checkExt($file['extension'])) {
$this->error = l('FILE_TYPE_NOT_ALLOW');
return false;
}
if(!$this->checkUpload($file['tmp_name'])) {
$this->error = l('INVALID_UPLOAD');
return false;
}
return true;
}
private function checkType($type)
{
$bln = true;
if(!empty($this->allowTypes))
$bln = in_array(strtolower($type),$this->allowTypes);
if($bln)
{
if(!empty($this->denyTypes))
$bln = !in_array(strtolower($ext),$this->denyTypes,true);
}
return $bln;
}
private function checkExt($ext)
{
$bln = true;
if(!empty($this->allowExts))
{
$bln = in_array(strtolower($ext),$this->allowExts,true);
}
if($bln)
{
if(!empty($this->denyExts))
$bln = !in_array(strtolower($ext),$this->denyExts,true);
}
return $bln;
}
private function checkSize($size)
{
return !($size >$this->maxSize) ||(-1 == $this->maxSize);
}
private function checkUpload($filename)
{
return is_uploaded_file($filename);
}
private function getExt($filename)
{
$pathinfo = pathinfo($filename);
return $pathinfo['extension'];
}
public function getUploadFileInfo()
{
return $this->uploadFileInfo;
}
public function getErrorMsg()
{
return $this->error;
}
}

?>